/*
 *
 * Version 1.00
 * 
 * Created on 2007-6-4
 * Encoding UTF-8
 */
package net.comm.blog.web.action;

import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.Globals;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import net.comm.blog.bean.BaseBlog;
import net.comm.blog.service.impl.BlogServiceImpl;
import net.comm.blog.web.form.BlogForm;
import net.comm.common.GlbFinal;
import net.comm.common.ReturnMessage;
import net.comm.friend.bean.BaseFriend;
import net.comm.friend.service.impl.FriendServiceImpl;
import net.comm.friend.service.impl.LinjuServiceImpl;
import net.comm.friend.web.form.FriendForm;
import net.comm.friend.web.form.LinjuForm;

import net.comm.util.HtmlMessageBox;
import net.comm.web.base.AbstractBaseAction;


/**
 * 博客Action
 * @author hcgong(hcgong@sina.com)
 * @version 1.0
 * @date 2007-6-4
 */
public class BlogAction extends AbstractBaseAction {
	
	private BlogServiceImpl service = null;
	
	/**
	 * struct-config中该ACTION中的每个forward的常量定义
	 */
	final String LIST_FORWARD    = "list";
	final String INFO_FORWARD    = "info";
	final String OPERATE_FORWARD = "oper";
	final String ACTIVITY_FORWARD = "activity";
	final String LOGIN_FORWARD = "loginFwd";		
	
	/**
	 *构造方法
	 */
	public BlogAction()
	{
		super(false);
		
		/**
		 * 初始化业务组件接口
		 */
		service = new BlogServiceImpl();
	}
	
	/**
	 * 默认动作
	 * 1.action值为空的时候,进入查询页面
	 * 2.每个操作都执行的代码,但是方法必须返回null
	 * @param mapping	 跳转的mapping值	
	 * @param request	 HttpServletRequest对象
	 * @param response	 HttpServletResponse对象
	 */
	public ActionForward startAction(ActionMapping mapping, ActionForm form,
									 HttpServletRequest request , 
									 HttpServletResponse response)
	{
		String action = getReqParam(request , ACTION_KEY);
		if(action == null || action.trim().length() == 0) {
			return mapping.findForward(LIST_FORWARD);
		} else {
			return null;
		}
	}
	
	/**
	 * 查询所有博客
	 * @param mapping		跳转的mapping值		
	 * @param request		HttpServletRequest对象
	 * @param response		HttpServletResponse对象
	 * @return
	 * @throws Exception
	 */
	public ActionForward list(ActionMapping mapping, ActionForm form,
							  HttpServletRequest request , 
							  HttpServletResponse response) 
		throws Exception
	{
		/**
		 *定义一个查询bean保存查询页面提交的查询条件
		 */
		BlogForm theForm = (BlogForm)form;
			
		// 生成分页对象
	
		theForm.setPage(this.getPageFromReq(request));
		
		ReturnMessage msg  = new ReturnMessage();
		
		/**
		 *调用services中的方法
		 */
		List list = service.list(theForm , msg);
		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
		{
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);
		}
		
		/**
		 *设置列表的数据
		 */
		setAction(request, "list");
		request.setAttribute("bloglist" , list);
		request.setAttribute("theForm", theForm);
		request.setAttribute("totalnum", theForm.getPage().getTotalNum());
		
		/**
		 * 设置分页对象,key,lastkey到request中
		 * 使用分页组件,必须设置这三个值
		 */
		request.setAttribute(PAGE_KEY , theForm.getPage());
		request.setAttribute(KEY , theForm.getKey());
		request.setAttribute(LAST_KEY , theForm.getLastKey());
	
		/**
		 * 转发到LIST_FORWARD指定的jsp文件
		 */
		return mapping.findForward(LIST_FORWARD);
	}
	/**
	 * 查询所有博客
	 * @param mapping		跳转的mapping值		
	 * @param request		HttpServletRequest对象
	 * @param response		HttpServletResponse对象
	 * @return
	 * @throws Exception
	 */
	public ActionForward adlist(ActionMapping mapping, ActionForm form,
							  HttpServletRequest request , 
							  HttpServletResponse response) 
		throws Exception
	{
		/**
		 *定义一个查询bean保存查询页面提交的查询条件
		 */
		BlogForm theForm = (BlogForm)form;
			
		// 生成分页对象
	
		theForm.setPage(this.getPageFromReq(request));
		
		ReturnMessage msg  = new ReturnMessage();
		
		/**
		 *调用services中的方法
		 */
		List list = service.adlist(theForm , msg);
		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
		{
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);
		}
		
		/**
		 *设置列表的数据
		 */
		setAction(request, "adlist");
		request.setAttribute("bloglist" , list);
		request.setAttribute("bcommentlist" , list);
		request.setAttribute("theForm", theForm);
		
		/**
		 * 设置分页对象,key,lastkey到request中
		 * 使用分页组件,必须设置这三个值
		 */
		request.setAttribute(PAGE_KEY , theForm.getPage());
		request.setAttribute(KEY , theForm.getKey());
		request.setAttribute(LAST_KEY , theForm.getLastKey());
	
		//取得动态评论
		
		/**
		 * 转发到LIST_FORWARD指定的jsp文件
		 */
		return mapping.findForward(LIST_FORWARD);
	}	
	/**
	 * 查询博客及评论
	 * @param mapping		跳转的mapping值		
	 * @param request		HttpServletRequest对象
	 * @param response		HttpServletResponse对象
	 * @return
	 * @throws Exception
	 */
	public ActionForward info(ActionMapping mapping, ActionForm form,
							  HttpServletRequest request , 
							  HttpServletResponse response) 
		throws Exception
	{
		/**
		 *定义一个查询bean保存查询页面提交的查询条件
		 */
		BlogForm theForm = (BlogForm)form;
		
		theForm.setBlogID(theForm.getQryBlogID());
		theForm.setFollID(0);
		theForm.setUpFollID(0);
		
		// 生成分页对象
		theForm.setPage(this.getPageFromReq(request));
		
		ReturnMessage msg  = new ReturnMessage();
		
		if(theForm.getPage().getCurPage() == 1){
			//取得日志信息
			service.get(theForm, msg);
			if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
			{
				HtmlMessageBox hmb = new HtmlMessageBox();
				hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
				hmb.setPrintMessage(msg.getMsg());
				hmb.setForwardUrl("javascript:history.go(-1)");
				return showMsg(hmb , request , response , mapping);
			}
			
			//取得当前浏览会员信息
			Integer userID = (Integer)request.getSession().getAttribute(GlbFinal.SESSION_USERID);
			if(userID == null){
				userID = 0;
			}
			if(theForm.getUserID() != userID){
				String isFriend = GlbFinal.NO;
				//判断当前浏览会员是否是好友
				FriendServiceImpl friendservice = new FriendServiceImpl();
				
				if(friendservice.isFriend(theForm.getUserID(), userID)) 
				{
					isFriend = GlbFinal.YES;	
				}else{
					isFriend = GlbFinal.NO;	
					msg.setErrCode(GlbFinal.ERRCODE_SUCCESS);
				}
				
				String isLinju = GlbFinal.NO;
				//判断是否是邻居
				LinjuServiceImpl linjuservice = new LinjuServiceImpl();
				LinjuForm linjuForm = new LinjuForm();
				
				linjuForm.setUserID(theForm.getUserID());
				linjuForm.setLinjuID(userID);
				
				linjuservice.get(linjuForm, msg);
				if(msg.getErrCode() == GlbFinal.ERRCODE_SUCCESS && linjuForm.getStatus().equals(GlbFinal.STS_NORMAL)) 
				{
					isLinju = GlbFinal.YES;	
				}else{
					isLinju = GlbFinal.NO;	
					msg.setErrCode(GlbFinal.ERRCODE_SUCCESS);
				}

				if(theForm.getBlogSet().equals(GlbFinal.USERSET_FRIEND) && isFriend.equals(GlbFinal.NO)){
					msg.setMsg("日志仅对好友开放！");
					HtmlMessageBox hmb = new HtmlMessageBox();
					hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
					hmb.setPrintMessage(msg.getMsg());
					hmb.setForwardUrl("javascript:history.go(-1)");
					return showMsg(hmb , request , response , mapping);
				}else if(theForm.getBlogSet().equals(GlbFinal.USERSET_LINJU) && isFriend.equals(GlbFinal.NO)){
					msg.setMsg("日志仅对邻居开放！");
					HtmlMessageBox hmb = new HtmlMessageBox();
					hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
					hmb.setPrintMessage(msg.getMsg());
					hmb.setForwardUrl("javascript:history.go(-1)");
					return showMsg(hmb , request , response , mapping);
				}else if(theForm.getBlogSet().equals(GlbFinal.USERSET_BAOMI) ){
					msg.setMsg("日志不开放！");
					HtmlMessageBox hmb = new HtmlMessageBox();
					hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
					hmb.setPrintMessage(msg.getMsg());
					hmb.setForwardUrl("javascript:history.go(-1)");
					return showMsg(hmb , request , response , mapping);
				}					
			}			
		}
		
		//取得日志评论
		List list = service.listallcomment(theForm, msg);
		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
		{
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);
		}
		
		/**
		 *设置列表的数据
		 */
		setAction(request, "info");
		request.setAttribute("followlist" , list);
		request.setAttribute("blogForm", theForm);
		request.setAttribute("theForm", theForm);
		
		/**
		 * 设置分页对象,key,lastkey到request中
		 * 使用分页组件,必须设置这三个值
		 */
		request.setAttribute(PAGE_KEY , theForm.getPage());
		request.setAttribute(KEY , theForm.getKey());
		request.setAttribute(LAST_KEY , theForm.getLastKey());
	
		//取得动态评论
		
		/**
		 * 转发到LIST_FORWARD指定的jsp文件
		 */
		return mapping.findForward(INFO_FORWARD);
	}	
	/**
	 * 跳转到明细页面
	 */
	public ActionForward get(ActionMapping mapping, ActionForm form,
		 	 					HttpServletRequest request , 
		 	 					HttpServletResponse response) 
		throws Exception
	{

		/**
		 *定义数据封装类保存提交的数据
		 */
		BlogForm theForm = (BlogForm)form;
		ReturnMessage msg  = new ReturnMessage();

		service.get(theForm , msg);
		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
		{
			/**
			 * 出现错误,则调用如下方法把错误信息提示到客户端,该部分
			 * 是使用response.write方法把相关的html代码写到客户端浏览器
			 * 1.hmb.setAlertType 提示类型(对话框,页面.....)
			 * 2.hmb.setPrintMessage 提示显示的信息
			 * 3.hmb.setForwardUrl 提示后跳转的URL
			 */
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);
		}
	
		theForm.setAction(getReqParam(request, "action"));
		request.setAttribute("theForm" , theForm);
		
		
		return mapping.findForward(OPERATE_FORWARD);
	}
	
	/**
	 * 跳转到添加页面
	 */
	public ActionForward addPage(ActionMapping mapping, ActionForm form,
			 				 	 HttpServletRequest request , 
			 				 	 HttpServletResponse response)  
		throws Exception
	{
		BlogForm theForm = (BlogForm)form;
		theForm.setAction(getReqParam(request, "action"));
		theForm.setFollID(0);
		theForm.setUpFollID(0);
		theForm.setReplyFlg(GlbFinal.ALLOW_FLG_ANY);
		theForm.setBlogSet(GlbFinal.USERSET_MEMBER);
		
		request.setAttribute("theForm", theForm);
	
		return mapping.findForward(OPERATE_FORWARD);
	}
	
	/**
	 * 跳转到编辑页面
	 */
	public ActionForward modifyPage(ActionMapping mapping, ActionForm form,
							 	    HttpServletRequest request , 
							 	    HttpServletResponse response)
		throws Exception
	{
	
		return get(mapping , form, request , response);
	}
	
	/**
	 *添加新博客
	 */
	public ActionForward add(ActionMapping mapping, ActionForm form,
							 HttpServletRequest request , 
							 HttpServletResponse response)
		throws Exception
	{
		BlogForm theForm = (BlogForm)form;

		ReturnMessage msg  = new ReturnMessage();
		String path = request.getSession().getServletContext().getRealPath("/");
		
		service.add(theForm,path, msg);
		
		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
		{
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);
		}else{
			msg.setMsg("日志发布成功！");
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_PRINTTOHTML);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl(GlbFinal.FOLDER_INSTALL + "/users.do?action=main");
			return showMsg(hmb , request , response , mapping);
		}
	}

	/**
	 *添加新博客评论
	 */
	public ActionForward addcomment(ActionMapping mapping, ActionForm form,
							 HttpServletRequest request , 
							 HttpServletResponse response)
		throws Exception
	{
		BlogForm theForm = (BlogForm)form;

		ReturnMessage msg  = new ReturnMessage();
		String path = request.getSession().getServletContext().getRealPath("/");
		
		service.addcomment(theForm,path, msg);
		
		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
		{
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);
		}
		theForm.setQryBlogID(theForm.getBlogID());
		
		request.setAttribute("theForm", theForm);
		
		return info(mapping , form, request , response);
	}
	
	/**
	 * 修改博客
	 */
	public ActionForward update(ActionMapping mapping, ActionForm form,
							 	HttpServletRequest request , 
							 	HttpServletResponse response)
		throws Exception
	{

		BlogForm theForm = (BlogForm) form;

		ReturnMessage msg  = new ReturnMessage();
		
		String path = request.getSession().getServletContext().getRealPath("/");

		service.update(theForm ,path, msg);
		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
		{
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);
		}
		theForm.setQryUserID(theForm.getUserID());
		
		request.setAttribute("theForm", theForm);
		
		return list(mapping , form, request , response);
	}

	/**
	 * 修改博客
	 */
	public ActionForward updsts(ActionMapping mapping, ActionForm form,
							 	HttpServletRequest request , 
							 	HttpServletResponse response)
		throws Exception
	{

		BlogForm theForm = (BlogForm) form;

		ReturnMessage msg  = new ReturnMessage();
		
		service.updsts(theForm ,msg);
		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
		{
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);
		}
		theForm.setQryUserID(theForm.getUserID());
		
		request.setAttribute("theForm", theForm);
		
		return list(mapping , form, request , response);
	}

	/**
	 *删除博客及回复
	 */
	public ActionForward delall(ActionMapping mapping, ActionForm form,
							 HttpServletRequest request , 
							 HttpServletResponse response)
		throws Exception
	{
		BlogForm theForm = (BlogForm) form;
		
		ReturnMessage msg  = new ReturnMessage();
		String path = request.getSession().getServletContext().getRealPath("/");
		
		service.delall(theForm ,path, msg);
		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
		{
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);
		}
		theForm.setQryUserID(theForm.getUserID());
		
		request.setAttribute("theForm", theForm);
		
		return list(mapping , form, request , response);
	}
	
	/**
	 *删除单个博客回复
	 */
	public ActionForward del(ActionMapping mapping, ActionForm form,
							 HttpServletRequest request , 
							 HttpServletResponse response)
		throws Exception
	{
		BlogForm theForm = (BlogForm) form;
		
		ReturnMessage msg  = new ReturnMessage();
		String path = request.getSession().getServletContext().getRealPath("/");
		
		service.delete(theForm ,path, msg);
		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
		{
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);
		}
		theForm.setQryBlogID(theForm.getBlogID());
		
		request.setAttribute("theForm", theForm);
		
		return info(mapping , form, request , response);
	}

	/**
	 * 中文英文切换页面
	 */
	public ActionForward tran(ActionMapping mapping, ActionForm form,
							  HttpServletRequest request , 
							  HttpServletResponse response)
	{
		HttpSession session = request.getSession(true);
		String language = getReqParam(request , "language");
		
		if("en".equals(language)) {
			session.setAttribute(Globals.LOCALE_KEY , Locale.ENGLISH);
		} else {
			session.setAttribute(Globals.LOCALE_KEY , Locale.CHINESE);
		}
		
		return mapping.findForward("tran");
	}
	
	
	/**
	 * 
	 */
	public ActionForward dis(ActionMapping mapping, ActionForm form,
							 HttpServletRequest request , 
							 HttpServletResponse response)
		throws Exception
	{
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
			hmb.setPrintMessage("msg.getMsg()");
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);
	}	 

}
